﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using RookieLibSharp.Calendar;
namespace BirthdayRemainder
{
    public class PersonInfo
    {
        public string Name { get; set; }
        private string _Birthday;
        public string Birthday 
        {
            get
            {
                return _Birthday;
            }
            set
            {
                _Birthday = value;
                GetOtherInfo();
            }
        }
        public int Age { get; set; }
        public string Phone { get; set; }
        public int DayToBirthday { get; set; }
        public bool IsLunar { get; set; }
        public string SolorOrLunar { get; set; }
        public bool DisplayBackColor { get; set; }
        /// <summary>
        /// 如果是阴历的话，需要判断日期是否有效，阴历有可能当前年没有
        /// </summary>
        public bool HasBirthday { get; protected set; }
        public PersonInfo()
        {
            DisplayBackColor = false;
        }
        private void GetOtherInfo()
        {
            SolorOrLunar = IsLunar ? "阴历" : "阳历";
            GetDayToBirthday();
        }
        public string GetSaveStr()
        {
            return string.Format("{0},{1},{2},{3}", this.Name, this.Birthday, this.SolorOrLunar, this.Phone);
        }
        private void GetDayToBirthday()
        {
            DateTime dtBirth = Convert.ToDateTime(Birthday);
            this.Age = DateTime.Today.Year - dtBirth.Year;
            DateTime dtNextBirth;
            HasBirthday = true;
            int iYearOfst;
            if (IsLunar)
            {
                for (iYearOfst = -2; iYearOfst < 20;iYearOfst++)
                {
                    if (ChineseCalendar.LunarDateIsValid(DateTime.Today.Year + iYearOfst, dtBirth.Month, dtBirth.Day))
                    {
                        dtNextBirth = ChineseCalendar.GetDateFromLunarDate(DateTime.Today.Year + iYearOfst, dtBirth.Month, dtBirth.Day, false);
                        if (dtNextBirth > DateTime.Today)
                        {
                            break;
                        }       
                    }
                }
                if (iYearOfst < 20)
                {
                    //20年之内有生日
                    dtNextBirth = ChineseCalendar.GetDateFromLunarDate(DateTime.Today.Year + iYearOfst, dtBirth.Month, dtBirth.Day, false);
                }
                else
                {
                    //20年都没有生日，就不过生日了
                    HasBirthday = false;
                    return;
                }
            }
            else
            {
                for (iYearOfst = 0; iYearOfst < 20; iYearOfst++)
                {
                    if (Funcs.DateTimeIsValid(DateTime.Today.Year + iYearOfst, dtBirth.Month, dtBirth.Day))
                    {
                        break;
                    }
                }
                if (iYearOfst < 20)
                {
                    //20年之内有生日
                    dtNextBirth = new DateTime(DateTime.Today.Year + iYearOfst, dtBirth.Month, dtBirth.Day);
                }
                else
                {
                    //20年都没有生日，就不过生日了
                    HasBirthday = false;
                    return;
                }
                
            }
            if (dtNextBirth<DateTime.Today)
            {
                dtNextBirth = dtNextBirth.AddYears(1);
                DayToBirthday = (int)(dtNextBirth - DateTime.Today).TotalDays;
            }
            else
            {

                DayToBirthday = (int)(dtNextBirth - DateTime.Today).TotalDays;
            }
            DisplayBackColor = DayToBirthday + (int)DateTime.Today.DayOfWeek < 7;
        }
        public StPersonInfo GetStruct()
        {
            StPersonInfo st = new StPersonInfo();
            st.Age = this.Age;
            st.Birthday = this.Birthday;
            st.DayToBirthday = this.DayToBirthday;
            st.IsLunar = this.IsLunar;
            st.Name = this.Name;
            st.Phone = this.Phone;
            st.SolorOrLunar = this.SolorOrLunar;
            st.HasBirthday = this.HasBirthday;
            return st;
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct StPersonInfo
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string Name;
        [MarshalAs(UnmanagedType.LPStr)]
        public string Birthday;
        public int Age;
        [MarshalAs(UnmanagedType.LPStr)]
        public string Phone;
        public int DayToBirthday;
        public bool DisplayBackColor;
        public bool IsLunar;
        [MarshalAs(UnmanagedType.LPStr)]
        public string SolorOrLunar;
        public bool HasBirthday;
    }
    public class PersonInfoComparer : IComparer<PersonInfo>
    {
        public int Compare(PersonInfo infoX, PersonInfo infoY)
        {
            if (infoX.DayToBirthday > infoY.DayToBirthday)
            {
                return 1;
            }
            else if (infoX.DayToBirthday == infoY.DayToBirthday)
            {
                return 0;
            }
            {
                return -1;
            }
        }
    }
}
